var exp = module.exports;

//获取今日零点的服务器时间戳
exp.zeroClockUnix = function () {
    var unix = new Date(new Date().toLocaleDateString()).getTime().toString();
    return unix.substr(0, 10);
}

//获取今日零点的服务器时间戳
exp.todayZeroClockUnix = function () {
    return new Date(new Date().toLocaleDateString()).getTime().toString();
}

//获取昨日零点的服务器时间戳
exp.yesterdayZeroClockUnix = function () {
    return exp.todayZeroClockUnix() - 86400000;
}

//获取当前的服务器时间戳
exp.currentUnix = function () {
    return Math.round(new Date().getTime() / 1000);
}

//检测是否是新的一天
exp.isNewDay = function (t) {
    return t < zeroClockUnix();
}


function getNow() {
    return new Date();
}

function getNowMonth() {
    return getNow().getMonth();
}

function getNowYear() {
    let nowYear = getNow().getFullYear();
    nowYear += (nowYear < 2000) ? 1900 : 0; //
    return nowYear;
}

function getLastMonthDate() {
    let now = getNow();
    now.setDate(1);
    now.setMonth(now.getMonth() - 1);
    return now;
}

function getLastYear() {
    return getLastMonthDate().getFullYear()
}

function getLastMonth() {
    return getLastMonthDate().getMonth()
}


//格局化日期：yyyy-MM-dd
function formatDate(date) {
    if (!date) {
        date = getNow();
    } else {
        date = new Date(date);
    }
    var myyear = date.getFullYear();
    var mymonth = date.getMonth() + 1;
    var myweekday = date.getDate();

    if (mymonth < 10) {
        mymonth = "0" + mymonth;
    }
    if (myweekday < 10) {
        myweekday = "0" + myweekday;
    }
    return (myyear + "-" + mymonth + "-" + myweekday);
}

/*
* Unix时间戳转换成日期格式  FormatDateTime("1497232433000")
* @param UnixTime Unix时间戳
* @return yyyy-MM-dd HH:mm:ss
*/
exp.unixToDate = function (UnixTime) {
    let date = new Date(parseInt(UnixTime));
    // let y = date.getFullYear();
    // let m = date.getMonth() + 1;
    // m = m < 10 ? ('0' + m) : m;
    // let d = date.getDate();
    // d = d < 10 ? ('0' + d) : d;
    // return y + '-' + m + '-' + d;
    return date;
};

//获得某月的天数
function getMonthDays(myMonth) {
    let nowYear = getNowYear();
    var monthStartDate = new Date(nowYear, myMonth, 1);
    var monthEndDate = new Date(nowYear, myMonth + 1, 1);
    var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
    return days;
}

//获得本季度的开端月份
getQuarterStartMonth = function () {
    let nowMonth = getNowMonth();
    var quarterStartMonth = 0;
    if (nowMonth < 3) {
        quarterStartMonth = 0;
    }
    if (2 < nowMonth && nowMonth < 6) {
        quarterStartMonth = 3;
    }
    if (5 < nowMonth && nowMonth < 9) {
        quarterStartMonth = 6;
    }
    if (nowMonth > 8) {
        quarterStartMonth = 9;
    }
    return quarterStartMonth;
}

//获得本周的开端日期
exp.getWeekStartDate = function (dataString) {
    // var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
    // return formatDate(weekStartDate);
    var weekDays = exp.getWeekDay(dataString);
    return weekDays[0]
}

//获得本周的停止日期
exp.getWeekEndDate = function (dataString) {
    // var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
    // return formatDate(weekEndDate);
    var weekDays = exp.getWeekDay(dataString);
    return weekDays[6];
}

//获得本月的开端日期
exp.getMonthStartDate = function () {
    let nowYear = getNowYear();
    let nowMonth = getNowMonth();
    var monthStartDate = new Date(nowYear, nowMonth, 1);
    return formatDate(monthStartDate);
}

//获得本月的停止日期
exp.getMonthEndDate = function () {
    let nowYear = getNowYear();
    let nowMonth = getNowMonth();
    var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
    return formatDate(monthEndDate);
}

//获得上月开端时候
exp.getLastMonthStartDate = function () {
    let nowYear = getNowYear();
    let lastMonth = getLastMonth();
    var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
    return formatDate(lastMonthStartDate);
}

//获得上月停止时候
exp.getLastMonthEndDate = function () {
    let nowYear = getNowYear();
    let lastMonth = getLastMonth();
    var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
    return formatDate(lastMonthEndDate);
}

//获得本季度的开端日期
exp.getQuarterStartDate = function () {
    let nowYear = getNowYear();
    var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
    return formatDate(quarterStartDate);
}

//获得本季度的停止日期
exp.getQuarterEndDate = function () {
    let nowYear = getNowYear();
    var quarterEndMonth = getQuarterStartMonth() + 2;
    var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));
    return formatDate(quarterStartDate);
}

//获得今天日期
exp.getTodayDate = function () {
    let now = getNow();
    return formatDate(now)
}

function getDay(num, str) {
    var today = new Date();
    var nowTime = today.getTime();
    var ms = 24 * 3600 * 1000 * num;
    today.setTime(parseInt(nowTime + ms));
    var oYear = today.getFullYear();
    var oMoth = (today.getMonth() + 1).toString();
    if (oMoth.length <= 1) oMoth = '0' + oMoth;
    var oDay = today.getDate().toString();
    if (oDay.length <= 1) oDay = '0' + oDay;
    return oYear + str + oMoth + str + oDay;
}

// 格式化日期
function dayformatDate(date) {
    date = new Date(date);
    let myyear = date.getFullYear();
    let mymonth = date.getMonth() + 1;
    let myweekday = date.getDate();
    mymonth < 10 ? mymonth = "0" + mymonth : mymonth;
    myweekday < 10 ? myweekday = "0" + myweekday : myweekday;
    return `${myyear}-${mymonth}-${myweekday}`;
}

// 获取当前月的天数
exp.mGetDate = function () {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var d = new Date(year, month, 0);
    return d.getDate();
}
/**
 * 获取本周的日期
 * @param {*} dateString 日期字符串 例如2020-07-08，不传用当日时间
 */
exp.getWeekDay = function (dateString) {
    // let dateString = dayformatDate(new Date());//当天的日期，例如2020-2-28
    if (!dateString) {
        dateString = dayformatDate(new Date());
    }
    let presentDate = new Date(dateString);
    let today = presentDate.getDay() !== 0 ? presentDate.getDay() : 7;

    return Array.from(new Array(7), function (val, index) {
        return dayformatDate(new Date(presentDate.getTime() - (today - index - 1) * 24 * 60 * 60 * 1000));
    });
}

// 获取当前月的所有日期
function getNowM(e) {
    let now = new Date();
    let current_month_num = mGetDate();
    let current_month = [];
    for (let i = 1; i <= current_month_num; i++) {
        let day = now.setDate(i);
        let everyDay = dayformatDate(day);
        current_month.push(everyDay);
    }
    return current_month;
}

//获取今天往前几天时间戳
exp.todaytoTime = function (day) {
    let today = (new Date()).getTime();
    return (today - day * 24 * 60 * 60 * 1000);
}

//格式化unix时间戳
exp.dateFormat = function (fmt, date) {
    let ret;
    const opt = {
        "Y+": date.getFullYear().toString(),        // 年
        "m+": (date.getMonth() + 1).toString(),     // 月
        "d+": date.getDate().toString()             // 日
        // 有其他格式化字符需求可以继续添加，必须转化成字符串
    };
    for (let k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
        }
        ;
    }
    ;
    return fmt;
}

//格林尼治时间转换北京时间
exp.gmtToCst = function (dateForm) {
    if (dateForm === "") {  //解决deteForm为空传1970-01-01 00:00:00
        return "";
    } else {
        var dateee = new Date(dateForm).toJSON();
        var date = new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
        return date;
    }
}
